package com.hnkywl.sts.service.tjbb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.report.CwsfcDao;
import com.hnkywl.sts.dao.tjbb.YwsfcDao;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-26
 * Time: 下午12:45
 * To change this template use File | Settings | File Templates.
 */
@Service
public class YwsfcService {
    private YwsfcDao ywsfcDao;

    public String ywsfcList(Integer gsid,  Date begindate, Date enddt,Integer wz_type,String wzdl, Integer ck_id,
                            Integer pageNo, Integer pageSize,Integer select_type, Integer dc_flag) {
        Map<String, Object> result = ywsfcDao.ywsfcList(gsid, begindate, enddt, wz_type,  wzdl,ck_id,pageNo, pageSize,select_type, dc_flag);
        Integer total=(Integer)result.get("total");
        List<Map> rows = (List<Map>)result.get("rows");
        List<Map> footer = (List<Map>)result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson +",\"total\":"+total+ "}";
        return json;
    }

    /**
     * 大屏展示：业务收发存报表
     */
    public Map<String, Object> ywsfcListForScreen(int pageNo,int pageSize) {
        Calendar calendar = Calendar.getInstance();
        //得到月初
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date dateStart = calendar.getTime();
        Map<String, Object> result = ywsfcDao.ywsfcList(null, dateStart, new Date(), 1,  null,null,pageNo, pageSize,1, null);
        return result;
    }

    /**
     * 本期入库采购
     * @param gsid
     * @param begindate
     * @param enddt
     * @param
     * @param select_type
     * @return
     */
    public String bqrkcg(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type ,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqrkcg(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 本期出库销售
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqckck(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqckck(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
        //List<Map> list = ywsfcDao.findbqckck(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 本期入库调整
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqrkrktz(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqrkrktz(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
       // List<Map> list = ywsfcDao.findbqrkrktz(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 本期出库调整
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqckcktz(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqckcktz(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
       // List<Map> list = ywsfcDao.findbqckcktz(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 本期入库拨入
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqrkzr(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqrkzr(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
       // List<Map> list = ywsfcDao.findbqrkzr(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     *  本期出库拨出
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqckzc(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqckzc(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
       // List<Map> list = ywsfcDao.findbqckzc(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 本期出库内部领用
     * @param gsid
     * @param select_type
     * @param
     * @param begindate
     * @param enddt
     * @return
     */
    public String bqcknbly(Integer gsid,Integer select_type,Integer ck_id,Integer wz_type,String pz, Date begindate, Date enddt) {
        Calendar c = Calendar.getInstance();
        c.setTime(enddt);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        enddt = c.getTime();
        List<Map> list = ywsfcDao.findbqcknbly(gsid,select_type, ck_id,wz_type, pz,begindate,c.getTime());
       // List<Map> list = ywsfcDao.findbqcknbly(gsid,select_type, ck_id, wz_type,pz,begindate,enddt);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }
                  //Integer gsid,  Date begindate, Date enddt,Integer wz_type,String wzdl, Integer ck_id,
                  //Integer pageNo, Integer pageSize,Integer select_type, Integer dc_flag
    public Map ywsfcListExport(Integer gsid, Date begindate, Date enddt,Integer wz_type, String wzdl,   Integer ck_id,
                               Integer select_type, Integer dc_flag){
        Map<String, Object> result = ywsfcDao.exportywsfcList(gsid, begindate,enddt,wz_type,wzdl,ck_id, 1,100000,select_type,dc_flag);
        return result;
    }
    @Autowired
    public void setYwsfcDao(YwsfcDao ywsfcDao) {
        this.ywsfcDao = ywsfcDao;
    }
}
